package com.xuesinuo.ots.util;

import java.util.List;

import com.google.common.collect.ImmutableList;

public class StringUtil {
    public static boolean isBlank(String str) {
        return str == null || str.isBlank();
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 随机字符串
     * 
     * @param sample 字符串样本
     * @param length 长度
     * @return 随机结果
     */
    public static String random(String sample, int length) {
        if (sample == null || sample.isEmpty() || length <= 0) {
            throw new IllegalArgumentException("Error params: StringUtil.random(" + sample + "," + length + ")");
        }
        List<Character> chars = ImmutableList.copyOf(sample.chars().mapToObj(c -> (char) c).iterator());
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int random = (int) (Math.random() * chars.size());
            result.append(chars.get(random));
        }
        return result.toString();
    }
}
